package service.impl;

import dao.MessageDao;
import dao.impl.MessageDaoImpl;
import domian.InformMessage;
import domian.Message;
import domian.OfficalMessage;
import service.MessageService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageServiceImpl implements MessageService {
    MessageDao messageDao = new MessageDaoImpl();


    @Override
    public Integer updateMessage(Message message) {
        Integer integer = messageDao.updateMessage(message);
        return integer;
    }

    @Override
    public Message findMessageById(Integer uid) {
        Message messageById = messageDao.findMessageById(uid);
        return messageById;
    }

    @Override
    public Map<String, List<String>> findAllMessageById(Integer id) {
        Map<String,List<String>> map=  new HashMap<>();
        //官方消息
        List<String> offList = new ArrayList<>();
        //通知消息
        List<String> informList = new ArrayList<>();
        //查询
        List<Message> allMessage = findAllMessage();


        //获取用户id
        Message message = null;
        for (Message message1 : allMessage) {
            if (id.equals(message1.getUid())){
               message= message1;
            }
        }
        //官方信息
        String mOffical = message.getM_offical();
        String[] split = mOffical.split("-");
        //拿到所有官方信息
        List<OfficalMessage> officalMessage = findOfficalMessage();
        for (String s : split) {
            Integer mid =Integer.parseInt(s);
            for (OfficalMessage officalMessage1 : officalMessage) {
                if (mid==officalMessage1.getMid()){
                    offList.add(officalMessage1.getMessage()+"?"+officalMessage1.getTime());
                }
            }
        }
        map.put("offical",offList);
        //拿到所有通知消息
        String mInform = message.getM_inform();
        String[] split1 = mInform.split("-");
        //拿到所有通知消息
        List<InformMessage> allInformMessage = findAllInformMessage();
        for (String s : split1) {
            int i = Integer.parseInt(s);
            for (InformMessage informMessage : allInformMessage) {
                if (i==informMessage.getUid()){
                    informList.add(informMessage.getMessage()+"?"+informMessage.getDate());
                }
            }
        }
        map.put("infrom",informList);


        //获取聊天好友列表
        List<String> f_list = new ArrayList<>();
        f_list.add(message.getFriend_id());
        map.put("friend",f_list);
        //获取黑名单列表
        List<String> list = new ArrayList<>();
        list.add(message.getBlacklist());
        map.put("black",list);
        return map;
    }

    @Override
    public List<Message> findAllMessage() {
        return messageDao.findAllMessage();
    }

    @Override
    public List<OfficalMessage> findOfficalMessage() {
        return messageDao.findOfficalMessage();
    }
    @Override
    public List<InformMessage> findAllInformMessage() {
        return messageDao.findAllInformMessage();
    }

}
